import { defineStore } from 'pinia'
import { addAddressList, getAddressList, editAddressList, delAddressList } from '@/api/address'
export const useAddressStore = defineStore('useAddressStore', {
  state() {
    return {
      addressList: [],
    }
  },
  //   请求
  actions: {
    // 添加数据
    async addAddressListHandler(params: object) {
      const result = await addAddressList(params)
      if ((result as any).code == 200) {
        //返回一个成功的promise
        return Promise.resolve("成功!")
      } else {
        //返回一个成功的promise
        return Promise.reject("失败!")
      }
    },

    //发送请求,获取地址列表的数据
    //TODO 获取地址信息
    async getAddressListHandler(data: object) {
      //发送请求
      const result = await getAddressList(data);
      if ((result as any).code == 200) {
        this.addressList = result.data
        //将地址信息存储到storage里面来
        localStorage.setItem("addressList", JSON.stringify(result.data))

      }
    },


    //TODO 修改
    async editAddressListHandler(data: object) {
      //发送请求
      const result = await editAddressList(data);
      if ((result as any).code == 200) {
        //返回一个成功的promise
        return Promise.resolve("成功!")
      } else {
        //返回一个成功的promise
        return Promise.reject("失败!")
      }
    },


    //TODO 删除地址信息
    async delAddressListHandler(data: { addressid: string }) {
      //发送请求
      const result = await delAddressList(data);
      if ((result as any).code == 200) {
        //返回一个成功的promise
        return Promise.resolve("成功!")
      } else {
        //返回一个成功的promise
        return Promise.reject("失败!")
      }
    },



  }
})
